<?	/*
	// File:	apache.wcp.phps
	// Purpose:	apache domain manipulation
	// Author:	Felix <webmaster@can-host.com>
	*/

// This has been moved to apache.mod.phps
/*
// Handle domains manipulation
if ($type == "domain") {
	
	// Generate apache configuration
	if ($data['action'] != 'remove') {
		unset($search);
		unset($replace);
		unset($searchpreg);
		unset($replacepreg);
		
		// Generate Apache configuration
		// open virthost.tpl
		$file = $cfg['basedir']."/httpd/templates/virthost.httpd.tpl";
		$httpdconf = stripslashes(fread(fopen($file, "r"), filesize($file)));
		
		// Replace variables by values
		$search[] = "{IP}";
		$replace[] = $data['ip'];
		
		$search[] = "{PORT}";
		$replace[] = $cfg['apache']['port'];
		
		$search[] = "{IP:PORT}";
		$IPArray = explode('|',$data['ip']);
		$IPString = '';
		foreach($IPArray as $IP) {
			$IPString .= $IP.':'.$cfg['apache']['port'].' ';
		}
		$replace[] = $IPString;
		
		$search[] = "{HOST.DOMAIN}";
		$replace[] = $data['host'].".".$data['domain'];
		
		$search[] = "{ADMINEMAIL}";
		$replace[] = $data['email'];

		// if the site is to be suspended, redirect the documentroot
		if ($data['action'] == 'suspend' OR $data['suspended'] != 'false')
			$path = $cfg['basedir']."/suspended";
		// else set correct path
		elseif ($data['type'] == "pointer")
			$path = $data['path'];
		else
			$path = $data['path']."/".$data['host']."/".$cfg['webname'];

		$search[] = "{PATH}";
		$replace[] = $path;
		
		// Set correct error logs
		if ($data['type'] == 'pointer') {
			$ownerdata = fetchdata("id,host","domain",$data['owner']);
			$log = $cfg['webdir']."/".$ownerdata['id']."/".$ownerdata['host']."/logs";
		}
		else
			$log = $cfg['webdir']."/".$data['id']."/".$data['host']."/logs";
		
		$search[] = "{LOGPATH}";
		$replace[] = $log;
		
		if ($cfg['bandwidth']) {
			$search[] = "[IF BANDWIDTH]";
			$replace[] = "";
			$search[] = "[ENDIF BANDWIDTH]";
			$replace[] = "";
			$search[] = "{BW_DIR}";
			$replace[] = $cfg['bandwidth_dir'];
			$search[] = "{BW_RATE}";
			$replace[] = ($data['bandwidth']*1024);
			$search[] = "{BW_PULSE}";
			$replace[] = $cfg['bandwidth_pulse'];
		} else {
			$searchpreg[] = "/\[IF BANDWIDTH\].*\[ENDIF BANDWIDTH\]/s";
			$replacepreg[] = "";
		}
		
		// If there are aliases
		if ($data['nohost'] == 'true' OR $data['aliases'] OR $data['catchall'] == 'true'){
			$search[] = "[IF ALIAS]";
			$replace[] = "";
			$search[] = "[ENDIF ALIAS]";
			$replace[] = "";
				
			$search[] = "{ALIASES}";		
			if ($data['aliases']) {
				$tmp = explode(" ",$data['aliases']);
				while (current($tmp)) {
					$aliaslist .= " ".current($tmp).".".$data['domain'];
					next($tmp);
				}
				$replace[] = $aliaslist;
			} else {
				$replace[] = "";
			}
			
			$search[] = "{NOHOST}";
			if ($data['nohost'] == 'true') {
				$replace[] = $data['domain'];
			} else {
				$replace[] = "";
			}

			$search[] = "{CATCHALL}";			
			if ($data['catchall'] == 'true') {
				$replace[] = " *.".$data['domain'];
			} else {
				$replace[] = "";
			}
		} else {
			$searchpreg[] = "/\[IF ALIAS\].*\[ENDIF ALIAS\]/s";
			$replacepreg[] = "";
		}
		
		// If domain redirect
		if ($data['redirect'] == 'true') {
			$search[] = "[IF DOMAINREDIRECT]";
			$replace[] = "";
			$search[] = "[ENDIF DOMAINREDIRECT]";
			$replace[] = "";
		} else {
			$searchpreg[] = "/\[IF DOMAINREDIRECT\].*\[ENDIF DOMAINREDIRECT\]/s";
			$replacepreg[] = "";
		}
		
		// Check for ServerSide Options (PHP, Perl, ASP, Python, etc)
		$cfgsslangarr = explode(',',$cfg['sslang']);
		foreach ($cfgsslangarr as $ssoption) {
			if ($data['serverside'][$ssoption] == 'on' AND $cfg['ss'][$ssoption])
				$ssoptions .= $cfg['ss'][$ssoption]."\n\n";
		}
		$search[] = "{SS_OPTIONS}";
		$replace[] = trim($ssoptions);
		
		$search[] = "{ALLOWOVERRIDE}";
		$replace[] = $cfg['allowoverride'];
		
		// check if web.cp uses SSL or not
		if ($cfg['ssl'])
			$http = "https://";
		else
			$http = "http://";
			
		// Set a correct port as well
		if ($cfg['port'])
			$port = ":".$cfg['port'];
		else
			$port = '';
		
		// Add web-cp redirections
		$search[] = "{PERSONALCP}";
		$replace[] = $http.$cfg['sysname'].$port."/?cp=personal&number=".$data['id'];
		
		$search[] = "{DOMAINCP}";
		$replace[] = $http.$cfg['sysname'].$port."/?cp=domain&number=".$data['id'];
		
		$search[] = "{RESELLERCP}";
		$replace[] = $http.$cfg['sysname'].$port."/?cp=reseller&number=".$data['id'];
		
		$search[] = "{SERVERCP}";
		$replace[] = $http.$cfg['sysname'].$port."/?cp=server";
		
		$search[] = "{WEBCP}";
		$replace[] = $http.$cfg['sysname'].$port."/";
			
		// Make Replacements
		for ($i=0; $i<sizeOf($searchpreg); $i++) {
			$httpdconf = preg_replace($searchpreg[$i], $replacepreg[$i], $httpdconf);
		}
		$httpdconf = str_replace($search, $replace, $httpdconf);
		
		// Parse %PATH% for real path. (used in php serverside config)
		if ($data['type'] == 'pointer')
			$httpdconf = str_replace("%PATH%",$cfg['webdir']."/".$data['owner'],$httpdconf);
		else
			$httpdconf = str_replace("%PATH%",$data['path'],$httpdconf);
			
		// If SSL is on and the appropriate .crt .key are present
		if ($data['type'] == 'pointer') {
			$ownerdata = fetchdata("id,host","domain",$data['owner']);
			$ssl = $cfg['webdir']."/".$ownerdata['id']."/".$ownerdata['host']."/ssl/";
		}
		else
			$ssl = $cfg['webdir']."/".$data['id']."/".$data['host']."/ssl/";
			
		$servercrt = "server.crt";
		$serverkey = "server.key";

		if ($data['serverside']['ssl'] == "on" AND file_exists($ssl.$servercrt) AND file_exists($ssl.$serverkey)) {
			// Generate Apache configuration
			// open virthost.tpl
			$file = $cfg['basedir']."/httpd/templates/virthostssl.httpd.tpl";
			$httpdsslconf = stripslashes(fread(fopen($file, "r"), filesize($file)));
			
			$search[] = "{SSLPORT}";
			$replace[] = 443;
			
			
			$search[] = "{IP:SSLPORT}";
			$IPArray = explode('|',$data['ip']);
			$IPString = '';
			foreach($IPArray as $IP) {
				$IPString .= $IP.':443 ';
			}
			$replace[] = $IPString;
			
			$search[] = "{SSLDIR}";
			$replace[] = $ssl;
			
			$search[] = "{SERVERCRT}";
			$replace[] = $servercrt;
			
			$search[] = "{SERVERKEY}";
			$replace[] = $serverkey;
			
			if (file_exists($ssl."intermediate.crt")) {
				$search[] = "[IF INTERMEDIATECRT]";
				$replace[] = "";
				$search[] = "[ENDIF INTERMEDIATECRT]";
				$replace[] = "";
			} else {
				$searchpreg[] = "/\[IF INTERMEDIATECRT\].*\[ENDIF INTERMEDIATECRT\]/s";
				$replacepreg[] = "";
			}
			
			// Make Replacements
			$httpdsslconf = str_replace($search, $replace, $httpdsslconf);
			for ($i=0; $i<sizeOf($searchpreg); $i++) {
				$httpdsslconf = preg_replace($searchpreg[$i], $replacepreg[$i], $httpdsslconf);
			}
			
		
			// Parse %PATH% for real path. (used in php serverside config)
			if ($data['type'] == 'pointer')
				$httpdsslconf = str_replace("%PATH%",$cfg['webdir']."/".$data['owner'],$httpdsslconf);
			else
				$httpdsslconf = str_replace("%PATH%",$data['path'],$httpdsslconf);
			
			$httpdconf .= $httpdsslconf;
		}
	}


	// Handle Domain Creation :: expect *
	if ($data['action'] == 'create') {
		
		// Write the apache config file
		$httpdinclude = $cfg['basedir']."/httpd/include/".$data['id'].".httpd";
		$fp = fopen($httpdinclude,"w+");
		fwrite($fp,$httpdconf,20480);
		fclose($fp);
		// Set permissions
		chmod($httpdinclude,0644);
		// Set HTTPD server restart
		$restarthttp = TRUE;

	}
	
	// Handle Domain updates :: expect *
	elseif ($data['action'] == 'update') {
		// Compare old and newly generated apache config file, write if necessary
		$httpdinclude = $cfg['basedir']."/httpd/include/".$data['id'].".httpd";
		$oldconf = implode("",file($httpdinclude));
		
		if ($oldconf != $httpdconf) {
			// Write the apache config file
			$fp = fopen($httpdinclude,"w+");
			fwrite($fp,$httpdconf,20480);
			fclose($fp);
			// Set permissions
			chmod($httpdinclude,0644);
			// Set HTTPD server restart
			$restarthttp = TRUE;
		}
	}
	
	// Suspend Domain :: expect 'id'
	elseif ($data['action'] == "suspend") {
		if ($data['id']) {
			// Write the apache config file
			$httpdinclude = $cfg['basedir']."/httpd/include/".$data['id'].".httpd";
			$fp = fopen($httpdinclude,"w+");
			fwrite($fp,$httpdconf,20480);
			fclose($fp);
			// Set permissions
			chmod($httpdinclude,0644);
			// Set HTTPD server restart
			$restarthttp = TRUE;
		}
	}
	
	// Unsuspend Domain :: expect 'id',
	elseif ($data['action'] == "unsuspend") {
		if ($data['id']) {
			// Write the apache config file
			$httpdinclude = $cfg['basedir']."/httpd/include/".$data['id'].".httpd";
			$fp = fopen($httpdinclude,"w+");
			fwrite($fp,$httpdconf,20480);
			fclose($fp);
			// Set permissions
			chmod($httpdinclude,0644);
			// Set HTTPD server restart
			$restarthttp = TRUE;
		}
	}
	
	// Remove Domain :: expect *
	elseif ($data['action'] == "remove") {
		if ($data['id']) {
	 		// Remove httpd config file
	 		$httpdinclude = $cfg['basedir']."/httpd/include/".$data['id'].".httpd";
	 		if (file_exists($httpdinclude))
	 			unlink($httpdinclude);
	 	}
	}
}
*/
?>
